var express = require('express');
var router = express.Router();
var db = require("../monmgoDB/db")
router.get('/findList', function (req, res, next) {
    var query = req.query;
    var pageSize = Number(query.pageSize) || 10, pageNum = Number(query.pageNum) || 1;//页面默认参数
    var searchData = {
        title: { $regex: new RegExp(query.title, "i") }, message: { $regex: new RegExp(query.message, "i") }
    }
    db.find(searchData).count(function (err, count) {
        if (!err) {
            var total = count;//数据总条数
            if (Math.ceil(total) < (pageSize * pageNum / 10)) {
                pageNum = 1;
                // skipNum = 0;
            }
            var skipNum = pageSize * (pageNum - 1);//从第几个开始查  跳过数据  limit 限制查多少条数据 sort 排序  exec 执行查找
            db.find(searchData).sort({ _id: -1 }).skip(skipNum).limit(pageSize).exec(function (error, data) {
                if (error) {
                    res.send({ code: 500, msg: error.message })
                } else {
                    var Sdata = {
                        code: 200,
                        data,
                        pageInfo: {
                            total, pageSize, pageNum
                        }
                    }
                    res.send(Sdata)
                }
            })
        } else {
            res.send({ code: 500, msg: err.message })
        }
    })



});
module.exports = router;
